package MyFitnessPlate.Handler;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import MyFitnessPlate.DataSet.ParsedGoalWeightDataSet;

public class GoalWeightHandler extends DefaultHandler {

	// ===========================================================
	// Fields
	// ===========================================================
	@SuppressWarnings("unused")
	private boolean in_xml = false;
	private boolean in_goal = false;

	private ParsedGoalWeightDataSet myParsedGoalWeightDataSet = new ParsedGoalWeightDataSet();

	// ===========================================================
	// Getter & Setter
	// ===========================================================
	public ParsedGoalWeightDataSet getParsedData() {
		return this.myParsedGoalWeightDataSet;
	}

	// ===========================================================
	// Methods
	// ===========================================================
	@Override
	public void startDocument() throws SAXException {
		this.myParsedGoalWeightDataSet = new ParsedGoalWeightDataSet();
	}

	@Override
	public void endDocument() throws SAXException {
		// Nothing to do
	}

	/**
	 * Gets be called on opening tags like: <tag> Can provide attribute(s), when
	 * xml was like: <tag attribute="attributeValue">
	 */
	@Override
	public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
		if (localName.equals("xml")) {
			this.in_xml = true;
		} else if (localName.equals("goal")) {
			this.in_goal = true;
		}
	}

	/**
	 * Gets be called on closing tags like: </tag>
	 */
	@Override
	public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
		if (localName.equals("xml")) {
			this.in_xml = false;
		} else if (localName.equals("goal")) {
			this.in_goal = false;
		}
	}

	/**
	 * Gets be called on the following structure: <tag>characters</tag>
	 */
	@Override
	public void characters(char ch[], int start, int length) {
		if (this.in_goal) {
			myParsedGoalWeightDataSet.setGoal(new String(ch, start, length).trim());
		}
	}
}